﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;

namespace System.Data.SugarClient
{
	public class SugarCommandBuilder : DbCommandBuilder
	{

		#region Constructor

		public SugarCommandBuilder() { }

		public SugarCommandBuilder(
			DbDataAdapter adapter)
		{
			base.DataAdapter = adapter;
			this.ConflictOption = ConflictOption.OverwriteChanges;
		}

		#endregion


		protected override void ApplyParameterInfo(DbParameter parameter, DataRow row, StatementType statementType, bool whereClause)
		{
			// throw new NotImplementedException();
		}

		protected override string GetParameterName(string parameterName)
		{
			return parameterName;
		}

		protected override string GetParameterName(int parameterOrdinal)
		{
			return "@" + parameterOrdinal.ToString();
		}

		protected override string GetParameterPlaceholder(int parameterOrdinal)
		{
			return "@" + parameterOrdinal.ToString();
		}

		/// <summary>
		/// Set the DataAdapter into this CommandBuilder.
		/// </summary>
		/// <param name="adapter"></param>
		protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
		{
			SugarDataAdapter oldAdapter = this.DataAdapter as SugarDataAdapter;
			SugarDataAdapter newAdapter = adapter as SugarDataAdapter;

			// remove old event handler (this.DataAdapter is in use)
			if (oldAdapter != null)
			{
				oldAdapter.RowUpdating -=
					new SugarRowUpdatingEventHandler(this.RowUpdatingHandler);
				return;
			}

			// add new event handler (this.DataAdapter is not in use)
			if (newAdapter != null)
				newAdapter.RowUpdating +=
					new SugarRowUpdatingEventHandler(this.RowUpdatingHandler);
		}  // SetRowUpdatingHandler

		private void RowUpdatingHandler(object sender, RowUpdatingEventArgs e)
		{
			base.RowUpdatingHandler(e);
		}


	}

	
}
